package com.huawei.esdk.uc.u19.north.rest.app.resource.ctc;

import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.apache.log4j.Logger;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.uc.common.ErrInfo;
import com.huawei.esdk.uc.domain.model.Conference;
import com.huawei.esdk.uc.loggermgr.LoggerMgr;
import com.huawei.esdk.uc.u19.business.professional.rest.ctc.CTCService;
import com.huawei.esdk.uc.u19.north.rest.app.resource.ctc.convert.CTCResourceConvert;
import com.huawei.esdk.uc.u19.north.rest.bean.ctc.ConferenceInfo;
import com.huawei.esdk.uc.u19.north.rest.bean.ctc.CreateConfResponse;
import com.huawei.esdk.uc.u19.north.rest.bean.ctc.ModifyConfResponse;
import com.huawei.esdk.uc.u19.north.rest.bean.ctc.QueryConfResponse;

@Path("uc/u19/book_conf")
public class CTCResource
{
    private static final Logger LOGGER = Logger.getLogger(CTCResource.class);
    
    private static final CTCResourceConvert ctcResourceConvert = new CTCResourceConvert();
    
    private static final CTCService ctcService = new CTCService();
    
    /**
     * 发起CTC会议
     * @param confInfo
     * @return
     */
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public CreateConfResponse createConf(ConferenceInfo confInfo)
    {
        CreateConfResponse response = new CreateConfResponse();
        if (null == confInfo || StringUtils.isEmpty(confInfo.getUserId()) || StringUtils.isEmpty(confInfo.getGwIp())
            || StringUtils.isEmpty(confInfo.getConfRoom()) || StringUtils.isEmpty(confInfo.getEnterPrompt())
            || StringUtils.isEmpty(confInfo.getLeavePrompt()) || StringUtils.isEmpty(confInfo.getRegSeatCount())
            || StringUtils.isEmpty(confInfo.getAttendPwd()) || StringUtils.isEmpty(confInfo.getChairmanPwd())
            || StringUtils.isEmpty(confInfo.getStartTime()) || StringUtils.isEmpty(confInfo.getEndTime())
            || StringUtils.isEmpty(confInfo.getSyncConfType()) || StringUtils.isEmpty(confInfo.getIsConfRecord())
            || StringUtils.isEmpty(confInfo.getConfName()) || StringUtils.isEmpty(confInfo.getSrtpUsageMode())
            || StringUtils.isEmpty(confInfo.getSpeakerCount()) || StringUtils.isEmpty(confInfo.getIsSuperConf())
            || StringUtils.isEmpty(confInfo.getSubPbxNo()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_COMPLETE_ERRORCODE));
            response.setResultDesc(ErrInfo.SDK_UC_PARAM_NOT_COMPLETE_ERRORCODESC);
            LoggerMgr.interfacelogger("POST", "createConf",
                confInfo, response.getResultCode());
            return response;
        }
        
        try
        {
            Conference reqconfInfo = ctcResourceConvert.getConferenceInfoRest2Model(confInfo);
            SDKResult<String> result = ctcService.createConf(reqconfInfo);
            
            response.setResultCode(String.valueOf(result.getErrCode()));
            response.setResultDesc(StringUtils.avoidNull(result.getDescription()));
            if (0 == result.getErrCode())
            {
                response.setConfId(result.getResult());
                response.setConfRoom(confInfo.getConfRoom());
            }
        }
        catch (SDKException e)
        {
            LOGGER.error("createConf method SDK error", e);
            response.setResultCode(String.valueOf(e.getSdkErrCode()));
            response.setResultDesc(StringUtils.avoidNull(e.getSdkErrDesc()));
        }
        catch (Exception e)
        {
            LOGGER.error("createConf method error", e);
            response.setResultCode(String.valueOf(ErrInfo.SDK_SYSTEM_ERRORCODE));
            response.setResultDesc(ErrInfo.SDK_SYSTEM_ERRORDESC);
            
        }
        
        LoggerMgr.interfacelogger("POST", "createConf",
            confInfo, response.getResultCode());
        
        return response;
    }
    
    /**
     * 修改CTC会议
     * @param confInfo
     * @return
     */
    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public ModifyConfResponse modifyConf(ConferenceInfo confInfo)
    {
        
        ModifyConfResponse response = new ModifyConfResponse();
        
        if (null == confInfo || StringUtils.isEmpty(confInfo.getUserId()) || StringUtils.isEmpty(confInfo.getGwIp())
            || StringUtils.isEmpty(confInfo.getConfRoom()) || StringUtils.isEmpty(confInfo.getEnterPrompt())
            || StringUtils.isEmpty(confInfo.getLeavePrompt()) || StringUtils.isEmpty(confInfo.getRegSeatCount())
            || StringUtils.isEmpty(confInfo.getAttendPwd()) || StringUtils.isEmpty(confInfo.getChairmanPwd())
            || StringUtils.isEmpty(confInfo.getStartTime()) || StringUtils.isEmpty(confInfo.getEndTime())
            || StringUtils.isEmpty(confInfo.getSyncConfType()) || StringUtils.isEmpty(confInfo.getIsConfRecord())
            || StringUtils.isEmpty(confInfo.getConfName()) || StringUtils.isEmpty(confInfo.getSrtpUsageMode())
            || StringUtils.isEmpty(confInfo.getSpeakerCount()) || StringUtils.isEmpty(confInfo.getIsSuperConf())
            || StringUtils.isEmpty(confInfo.getSubPbxNo()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_COMPLETE_ERRORCODE));
            response.setResultDesc(ErrInfo.SDK_UC_PARAM_NOT_COMPLETE_ERRORCODESC);
            LoggerMgr.interfacelogger("PUT", "modifyConf",
                confInfo, response.getResultCode());
            return response;
        }
        
        try
        {
            Conference reqconfInfo = ctcResourceConvert.getConferenceInfoRest2Model(confInfo);
            SDKResult<String> result = ctcService.modifyConf(reqconfInfo);
            
            response.setResultCode(String.valueOf(result.getErrCode()));
            response.setResultDesc(StringUtils.avoidNull(result.getDescription()));
        }
        catch (SDKException e)
        {
            LOGGER.error("modifyConf method SDK error", e);
            response.setResultCode(String.valueOf(e.getSdkErrCode()));
            response.setResultDesc(StringUtils.avoidNull(e.getSdkErrDesc()));
        }
        catch (Exception e)
        {
            LOGGER.error("modifyConf method error", e);
            response.setResultCode(String.valueOf(ErrInfo.SDK_SYSTEM_ERRORCODE));
            response.setResultDesc(ErrInfo.SDK_SYSTEM_ERRORDESC);
        }
        
        LoggerMgr.interfacelogger("PUT", "modifyConf",
            confInfo, response.getResultCode());
        
        return response;
    }
    
    /**
     * 查询CTC会议列表
     * @return
     */
    @GET
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public QueryConfResponse queryConf(@QueryParam("userId")
    String userId, @QueryParam("gwIp")
    String gwIp, @QueryParam("subPbxNo")
    String subPbxNo, @QueryParam("confRoom")
    String confRoom)
    {
        QueryConfResponse response = new QueryConfResponse();
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(gwIp))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_COMPLETE_ERRORCODE));
            response.setResultDesc(ErrInfo.SDK_UC_PARAM_NOT_COMPLETE_ERRORCODESC);
            LoggerMgr.interfacelogger("GET", "queryConf",
                null, response.getResultCode());
            return response;
        }
        
        // 校验gwIp参数的格式
        if (!StringUtils.isIp(gwIp))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
            response.setResultDesc(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
            LoggerMgr.interfacelogger("GET", "queryConf",
                null, response.getResultCode());
            return response;
        }
        
        try
        {
            SDKResult<List<Conference>> result = ctcService.queryConf(userId, gwIp, subPbxNo, confRoom);
            response = ctcResourceConvert.getQueryConfResponseModel2Rest(result);
        }
        catch (SDKException e)
        {
            LOGGER.error("queryConf method SDK error", e);
            response.setResultCode(String.valueOf(e.getSdkErrCode()));
            response.setResultDesc(StringUtils.avoidNull(e.getSdkErrDesc()));
        }
        catch (Exception e)
        {
            LOGGER.error("queryConf method error", e);
            response.setResultCode(String.valueOf(ErrInfo.SDK_SYSTEM_ERRORCODE));
            response.setResultDesc(ErrInfo.SDK_SYSTEM_ERRORDESC);
        }
        
        LoggerMgr.interfacelogger("GET", "queryConf",
            null, response.getResultCode());
        
        return response;
    }
}
